package com.ruoyi.project.rate.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.rate.Constants;
import com.ruoyi.project.rate.Utils;
import com.ruoyi.project.rate.domain.Sup;
import com.ruoyi.project.rate.service.SupService;

@Controller
@RequestMapping("/rate/sup")
public class SupController extends BaseController {
//	private static final String PAGE_FLAG_IN = "IN";
//	private static final String PAGE_FLAG_OUT = "OUT";
//	private static final String PAGE_FLAG_ALL = "ALL";
	
	@Autowired
	private SupService supService;

	@RequestMapping("/sup")
	public String sup(Sup sup, ModelMap mmap) {
		String pageFlag = sup.getPageFlag();
//		String type = sup.getType();
		if (StringUtils.isBlank(pageFlag)) {
			throw new RuntimeException("请求参数错误, pageFlag is blank.");
		}
		/**
		if (StringUtils.isBlank(type)) {
			throw new RuntimeException("请求参数错误, type is blank.");
		}
		**/
		/*
		 * if (Constants.PAGE_FLAG_SUBMIT.equals(pageFlag) && StringUtils.isBlank(type))
		 * { throw new RuntimeException("请求参数错误, type is blank."); }
		 */

		sup.setMonthId(Utils.currentMonth());
		mmap.put("sup", sup);

		mmap.put("deptList", Utils.level2DeptList());
		
		return "rate/supList";
	}

	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Sup sup) {
		String pageFlag = sup.getPageFlag();
		if (StringUtils.isBlank(pageFlag)) {
			throw new RuntimeException("请求参数错误, pageFlag is blank.");
		}
		/**
		if (Constants.PAGE_FLAG_SUBMIT.equals(pageFlag)) {
			sup.setCreateDept(ShiroUtils.getSysDept());
		} else if (Constants.PAGE_FLAG_KHZ.equals(pageFlag)) {
			// 考核组审核时只能看到本组的单据
			List<DictData> khzList = this.khzDeptService.getKhzDictDataDept(ShiroUtils.getSysDept());
			for (DictData dd : khzList) {
				sup.getKhzList().add(dd.getDictValue());
			}
			sup.getKhzList().add("EMPTY"); // 必须按照khz过滤
		} else if (Constants.PAGE_FLAG_FGLD.equals(pageFlag)) {
			// 监狱领导审核
			sup.setFgldChecker(ShiroUtils.getSysUser());
		}
		**/

		startPage();
		List<Sup> list = this.supService.findList(sup);

		return getDataTable(list);
	}

	@RequestMapping("/add")
	public String add(Sup sup, ModelMap mmap) {
		if (StringUtils.isBlank(sup.getType())) {
			throw new RuntimeException("type is blank.");
		}

		sup.setCreateDept(ShiroUtils.getSysDept());
		sup.setPageFlag(Constants.PAGE_FLAG_ADD);
		
		// 校验是否存在考核组
		/**
		List<DictData> khzList = Utils.getKhzListByPageFlag(sup.getPageFlag());
		if (khzList.size() == 0) {
			throw new RuntimeException("查询不到【" + sup.getCreateDept().getDeptName() + "】对应的考核组");
		}
		mmap.put("khzList", khzList);
		**/
		
		mmap.put("sup", sup);
		mmap.put("deptList", Utils.level2DeptList());

		return "rate/supForm";
	}

	@RequestMapping("/edit")
	public String edit(Sup sup, ModelMap mmap) {
		Sup s = this.supService.get(sup);
		if (s == null) {
			throw new RuntimeException("找不到对应的数据！");
		}
		s.setPageFlag(sup.getPageFlag());

		mmap.put("sup", s);
		mmap.put("deptList", Utils.level2DeptList());
//		mmap.put("khzList", Utils.getKhzListByPageFlag(s.getPageFlag()));
		
		// 审核时需要展示审核区域
		/**
		String showCheckResult = "0";
		if (StringUtils.equalsAny(s.getPageFlag(),
				Constants.PAGE_FLAG_KHZ,
				Constants.PAGE_FLAG_FGLD,
				Constants.PAGE_FLAG_HZ)) {
			showCheckResult = "1";
		}
		mmap.put("showCheckResult", showCheckResult);
		**/
		
		// 考核组审核时需要选择分管领导
		/**
		List<User> fgldList = new ArrayList<User>();
		if (StringUtils.equals(Constants.PAGE_FLAG_KHZ, s.getPageFlag())) {
			fgldList = Utils.fgldList();
		}
		mmap.put("fgldList", fgldList);
		**/
		
		return "rate/supForm";
	}

	@PostMapping("/save")
	@ResponseBody
	public AjaxResult save(Sup sup) {
		String pageFlag = sup.getPageFlag();
		String type = sup.getType();
		if (StringUtils.isBlank(type)) {
			throw new RuntimeException("type is blank.");
		}
		/**
		if (StringUtils.isBlank(sup.getKhz())) {
			throw new RuntimeException("khz is blank.");
		}
		**/
		
		if (StringUtils.isBlank(sup.getMonthId())) {
			throw new RuntimeException("month is blank.");
		}
		if (!StringUtils.equalsAny(pageFlag, "ADD", "EDIT")) {
			throw new RuntimeException("error pageFlag.");
		}
		
		if (sup.getDept() == null || sup.getDept().getDeptId() == null || sup.getDept().getDeptId() == 0L) {
			throw new RuntimeException("please select dept.");
		}
		
		// 校验分数值的有效性
		if (StringUtils.equals(Constants.SUP_TYPE_JF, sup.getType())) {
			if (sup.getScore() == null || sup.getScore().doubleValue() <= 0.0) {
				return AjaxResult.error("请输入大于0的分数");
			}
		} else if (StringUtils.equals(Constants.SUP_TYPE_KF, sup.getType())) {
			if (sup.getScore() == null || sup.getScore().doubleValue() >= 0.0) {
				return AjaxResult.error("请输入小于0的分数");
			}
		} else if (StringUtils.equalsAny(sup.getType(), Constants.SUP_TYPE_KHB, Constants.SUP_TYPE_SZPY)) {
			if (sup.getScore() == null || sup.getScore().doubleValue() == 0.0) {
				return AjaxResult.error("请输入不等于0的分数");
			}
		}
		
		sup.setStatus(Constants.KH_STATUS_APPROVED); // 默认审核通过
		if (StringUtils.isBlank(sup.getId())) {
			sup.setCreateDept(ShiroUtils.getSysDept());
		}
		
		this.supService.save(sup);

		return AjaxResult.success("保存成功！");
	}
	
	/**
	@RequestMapping("/submitToCheck")
	@ResponseBody
	public AjaxResult submitToCheck(Sup sup) {
		if (StringUtils.isBlank(sup.getId())) {
			throw new RuntimeException("请求参数id错误！");
		}

		sup = this.supService.get(sup);
		if (sup == null) {
			throw new RuntimeException("can not find sup！");
		}

		this.supService.submitToCheck(sup);

		return AjaxResult.success("提交审核成功！");
	}
	**/
	
	/**
	@PostMapping("/batchSubmitToCheck")
	@ResponseBody
	public AjaxResult batchSubmitToCheck(String ids, Sup sup) {
		if (StringUtils.isBlank(sup.getPageFlag())) {
			throw new RuntimeException("请求参数pageFlag为空！");
		}

		if (StringUtils.isBlank(ids)) {
			throw new RuntimeException("请求参数ids错误！");
		}
		
		int i = this.supService.batchSubmitToCheck(sup, ids);
		
		return AjaxResult.success("批量提交审核【" + i + "】条记录！");
	}
	**/
	
	/**
	@PostMapping("/submitCheck")
	@ResponseBody
	public AjaxResult submitCheck(Sup sup) {
		String pageFlag = sup.getPageFlag();
		if (StringUtils.isBlank(pageFlag)) {
			throw new RuntimeException("请求参数pageFlag为空！");
		}

		if (StringUtils.isBlank(sup.getId())) {
			throw new RuntimeException("请求参数id错误！");
		}
		
		this.supService.submitCheck(sup);

		return AjaxResult.success("审核成功！");
	}
	**/
	
	/**
	@PostMapping("/batchSubmitCheck")
	@ResponseBody
	public AjaxResult batchSubmitCheck(String ids, Sup sup) {
		if (StringUtils.isBlank(sup.getPageFlag())) {
			throw new RuntimeException("请求参数pageFlag为空！");
		}
		
		if (StringUtils.isBlank(sup.getCheckResult())) {
			throw new RuntimeException("请求参数checkResult为空！");
		}

		if (StringUtils.isBlank(ids)) {
			throw new RuntimeException("请求参数ids错误！");
		}
		
		if (Constants.PAGE_FLAG_KHZ.equals(sup.getPageFlag())) {
			if (sup.getFgldChecker() == null
					|| sup.getFgldChecker().getUserId() == null
					|| sup.getFgldChecker().getUserId() <= 0L) {
				throw new RuntimeException("请选择对应的分管领导！");
			}
		}
		
		int i = this.supService.batchSubmitCheck(sup, ids);
		
		return AjaxResult.success("批量审核成功【" + i + "】条记录！");
	}
	**/
	
	/**
	@RequestMapping("/cancelCheck")
	@ResponseBody
	public AjaxResult cancelCheck(Sup sup) {
		if (sup == null || StringUtils.isBlank(sup.getPageFlag())) {
			throw new RuntimeException("请求参数pageFlag为空！");
		}

		if (StringUtils.isBlank(sup.getId())) {
			throw new RuntimeException("请求参数id错误！");
		}
		
		this.supService.cancelCheck(sup);

		return AjaxResult.success("撤销审核成功！");
	}
	**/
    
    /**
     * 撤回报表
     * @param id
     * @param mmap
     * @return
     */
	/**
    @RequestMapping("/cancelSubmit")
    @ResponseBody
    public AjaxResult cancel(Sup sup) {
    	sup = this.supService.get(sup);
		if (sup == null) {
			throw new RuntimeException("找不到对应的数据！");
		}
		
		if (!Constants.KH_STATUS_KHZ.equals(sup.getStatus())) {
			throw new RuntimeException("当前申请状态不允许当前操作！");
		}
		
		this.supService.cancel(sup);
		
    	return AjaxResult.success("撤回成功！");
    }
    **/
	
	/**
    @PostMapping("/batchCancel")
	@ResponseBody
	public AjaxResult batchCancel(String ids, Sup sup) {
		if (StringUtils.isBlank(sup.getPageFlag())) {
			throw new RuntimeException("请求参数pageFlag为空！");
		}
		if (StringUtils.isBlank(ids)) {
			throw new RuntimeException("请求参数ids错误！");
		}
		int i = this.supService.batchCancel(sup, ids);
		
		return AjaxResult.success("批量撤回申请成功【" + i + "】条记录！");
	}
	**/
	
	@RequestMapping("/delete/{id}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") String id) {
    	if (StringUtils.isBlank(id)) {
    		throw new RuntimeException("请求参数错误：【id=" + id + "】");
    	}
    	
    	Sup sup = this.supService.get(id);
		if (sup == null) {
			throw new RuntimeException("找不到对应的数据！");
		}
		
		this.supService.delete(sup);
		
    	return AjaxResult.success("删除成功！");
	}
	
	/**
	 * 进入专项分查询界面
	 * @return
	 */
	/**
	@RequestMapping("/supListForCx")
	public String supListForCx(Sup sup, ModelMap mmap) {
		sup.setMonthId(Utils.lastMonth());
		mmap.put("sup", sup);
		return "rate/supListForCx";
	}
	**/

	/**
	 * 专项分查询
	 * @return
	 */
	/**
	@PostMapping("/listForCx")
	@ResponseBody
	public TableDataInfo listForCx(Sup sup) {
		return getDataTable(this.queryForCx(sup));
	}
	**/
	
	/**
	 * 专项分查询
	 * @return
	 */
	/**
	@ResponseBody
	public List<Sup> queryForCx(Sup sup) {
		if (StringUtils.isBlank(sup.getPageFlag())) {
			throw new RuntimeException("pageFlag is blank.");
		}
 		if (StringUtils.isBlank(sup.getMonthId())) {
			throw new RuntimeException("请选择考核月份.");
		}
		
		if (PAGE_FLAG_ALL.equals(sup.getPageFlag())) {
			// do nothing.
		} else if (PAGE_FLAG_IN.equals(sup.getPageFlag())) {
			// 查询当前部门被扣分的情况
			sup.setDept(ShiroUtils.getSysDept());
		} else if (PAGE_FLAG_OUT.equals(sup.getPageFlag())) {
			// 查询当前部门考核其他部门的扣分情况
			sup.setCreateDept(ShiroUtils.getSysDept());
		}
		
		// 只查询审核通过的考核信息
		sup.setStatus(Constants.KH_STATUS_APPROVED);
		List<Sup> supList = this.supService.findList(sup);
		
		// 同一个考核组的放一起
		Collections.sort(supList, new Comparator<Sup>() {
			@Override
			public int compare(Sup o1, Sup o2) {
				return o1.getKhz().compareTo(o2.getKhz());
			}
		});
		
		return supList;
	}
	**/
}